import {Component, OnInit} from "@angular/core";
import {LocalDataSource} from "ng2-smart-table";
import {SmartTableData} from "../../@core/data/smart-table";
import {NbDateService, NbDialogService} from "@nebular/theme";
import {ActivatedRoute} from "@angular/router";
import {AdminService} from "../../AdminService";
import {DocService, PowerService} from "../../energy/api";
import {BaseRequest} from "../../utils/BaseRequest";
import {map} from "rxjs/operators";
import {ResponseUnitInfo} from "../../energy/api/model/responseUnitInfo";
import {ResponseElectricConsumptionData} from "../../energy/api/model/responseElectricConsumptionData";
import {ResponseConsumptionData} from "../../energy/api/model/responseConsumptionData";

@Component({
  selector: 'ngx-smart-table',
  templateUrl: './other-history.component.html',
  styles: [`
    nb-card {
      transform: translate3d(0, 0, 0);
    }
    .margin {
      margin: 10px;
    }
  `],
})
export class OtherHistoryComponent implements OnInit{
  settings = {
    pager:{
      display:true,
    },
    hideSubHeader: true,
    actions: {
      columnTitle: '操作',
      add: false,
      delete: false,
      edit: false,
      position: 'right',
    },
    columns: {
      devNo: {
        title: '设备编号',
        type: 'string',
      },
      isPooled: {
        title: '是否公摊',
        type: 'string',
      },
      devType: {
        title: '设备类型',
        type: 'string',
      },
      beginMeterReading: {
        title: '启表码',
        type: 'number',
      },
      endMeterReading: {
        title: '终表码',
        type: 'number',
      },
      startTime:{
        title: '开始时间',
        type: 'string'
      },
      endTime: {
        title: '结束时间',
        type: 'string'
      },
      totalConsumption: {
        title: '总值',
        type: 'string'
      },
      extInfo: {
        title: '额外信息',
        type: 'string'
      }
    },
  };
  source: DataDataSource;
  devNo:string;
  queryType: string = "1";
  startTime: Date = new Date();
  endTime: Date = new Date();
  constructor(private service: SmartTableData
              ,private api:PowerService
              ,private dialogService: NbDialogService
              ,private route: ActivatedRoute
              ,private dateService: NbDateService<Date>
              ,private admin:AdminService) {

  }
  ngOnInit(): void {
    this.route.paramMap.subscribe(value=>{
      const devNo = value.get('deviceId');
      if (devNo != null) this.devNo = devNo;
      this.startTime.setDate(0);
      this.source = new DataDataSource(this.api,this.dateService,this.devNo,this.startTime,this.endTime);
    });

  }

  filter(){
    this.source.devNo = this.devNo;
    this.source.endTime = this.endTime;
    this.source.startTime = this.startTime;
    this.source.setPage(1);
    this.source.refresh();
  }
}

export class DataDataSource extends LocalDataSource {
  queryType: string = "1";
  all: number = -1;
  constructor(private api: PowerService, private dateService: NbDateService<Date>
              ,public devNo?:string
              ,public startTime?:Date
              ,public endTime?:Date) {
    super();
  }

  count(): number {
    return this.all;
  }

  getElements(): Promise<any> {
    const query = {
      pageIndex: (this.pagingConf['page'] - 1) * this.pagingConf['perPage'],
      pageSize: this.pagingConf['perPage'],
      devNo:this.devNo,
      queryType:this.queryType
    };
    if(this.startTime!=null)
      query['startTime'] = this.dateService.format(this.startTime, "yyyyMMdd");
    if(this.endTime!=null)
      query['endTime'] = this.dateService.format(this.endTime, "yyyyMMdd");
    return this.api.requestECAConsumptionData(new BaseRequest(query)).pipe(map((res:ResponseConsumptionData)=>{
      this.all = res.dataCount;
      return res.data;
    })).toPromise();
  }
}
